package study.spring.transaction;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

public class JdbcServiceTransationTemplate implements JdbcService {

	private JdbcTemplate template;

	private TransactionTemplate transaction;

	public void insert(final EditLink t) {

		String result = transaction.execute(new TransactionCallback<String>() {

			public String doInTransaction(TransactionStatus status) {

//				try {
					String sql = "INSERT INTO tbl_chouti_edit_link (name,img_url,url) VALUES ('6', '/images/image48.png', 'http://www.baidu.com')";
					template.execute(sql);

					if (null == t.getName()) {
						System.out.println("rollback2");
						throw new RuntimeException();
					}
					return "sucess";

//				} catch (Exception e) {
//					status.setRollbackOnly();
//					return "rollback";
//				}
			}
		});

		System.out.println(result);
	}

	// ============================
	public JdbcTemplate getTemplate() {
		return template;
	}

	public void setTemplate(JdbcTemplate template) {
		this.template = template;
	}

	public TransactionTemplate getTransaction() {
		return transaction;
	}

	public void setTransaction(TransactionTemplate transaction) {
		this.transaction = transaction;
	}

}
